<template>
	 <div :xl="6" :lg="7" class="bg-login">
    <!--标题-->
    <el-row type="flex" class="row-bg row-two" justify="center" align="middle">
      <el-col :span="6"></el-col>
      <el-col :span="6">
        <!--标题-->
        <h1 class="title">营养膳食系统——登录页面</h1>
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
    <!--form表单-->
    <el-row type="flex" class="row-bg card" justify="center" align="bottom">
      <el-col :span="7" class="login-card">
        <!--loginForm-->
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="21%" class="loginForm">
          <el-form-item label="账户" prop="username" style="width: 380px">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 380px">
            <el-input type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" style="width: 380px">
            <el-input v-model="loginForm.code" class="code-input" style="width: 70%;float: left"></el-input>
            <!--验证码图片-->
            <el-image :src="codeImg" class="codeImg" @click="getCaptcha()"></el-image>
          </el-form-item>
          <el-form-item class="btn-ground">
            <el-button type="primary" @click="submitForm('loginForm')">立即登陆</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
					  <router-link class="register" :to="{name:'Register'}">没有用户？点击注册</router-link>
          </el-form-item>
        </el-form>
      </el-col>
			
    </el-row>
		
  </div>

</template>

<script>

	import qs from 'qs'

	export default {
		name: "Login",
		data() {
			return {
				loginForm: {
					username: '',
					password: '',
					code: '',
					token: ''
				},
				rules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
					code: [
						{ required: true, message: '请输入验证码', trigger: 'blur' },
						{ min: 4, max: 4, message: '长度为 4 个字符', trigger: 'blur' }
					],
				},
				codeImg: null
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$axios.post('/login?'+ qs.stringify(this.loginForm)).then(res => {

							console.log(res)

							const jwt = res.headers['authorization']

							this.$store.commit('SET_TOKEN', jwt)
							this.$router.push("/index")
						})

					} else {
						console.log('error submit!!');
						return false;
					}
					this.getCaptcha()
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			getCaptcha() {
				this.$axios.get('/captcha').then(res => {
					this.loginForm.token = res.data.data.token
					this.codeImg = res.data.data.captchaImg
					this.loginForm.code = ''
				})
			}
		},
		created() {
			this.getCaptcha()
		}
	}
</script>

<style scoped>

	body{
  margin: 0px;
}
.codeImg {
  /*让验证码图片飘在右边*/
  float: right;
  /*设置一些圆角边框*/
  border-radius: 3px;
  /*设置宽度*/
  width: 30%;
	padding-top: 8px;
}


.bg-login {
  height: 100vh;
  background-size: 200%;
}

.btn-ground {
  text-align: center;
}

.logo {
  margin: 30px;
  height: 70px;
  width: 70px;
  position: fixed;
}

.title {
  text-shadow: -3px 3px 1px #5f565e;
  text-align: center;
  margin: 15% 0;
  color: #52a807;
  font-size: 50px;
	width: 800px;
	/*居中*/
	margin-left: 50%;
  transform: translateX(-50%);
}

.login-card {
  background-color: #fff;
  box-shadow: 0 0 20px gray;
  border-radius: 10px;
  padding: 40px 40px 30px 30px;
  width: auto;
}
.bg-login /deep/ .el-form-item__label {
	font-size: 18px;
	color: #000;
}

/* 底部注册 */
.register{
  text-decoration: none;
	color: black;
	display: block;
	width: 254px;
	margin-top: 20px;
}
.register:hover{
  color: #52a807;
	font-weight: 700;
}

</style>